package moyangjia.retailers.storage.config;

import com.alibaba.druid.pool.DruidDataSource;
import com.baomidou.mybatisplus.core.MybatisConfiguration;
import com.baomidou.mybatisplus.extension.plugins.PaginationInterceptor;
import com.baomidou.mybatisplus.extension.spring.MybatisSqlSessionFactoryBean;
import org.apache.ibatis.plugin.Interceptor;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.boot.jdbc.DataSourceBuilder;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;
import org.springframework.transaction.PlatformTransactionManager;
import org.springframework.transaction.annotation.EnableTransactionManagement;
import org.springframework.transaction.annotation.TransactionManagementConfigurer;
import org.springframework.validation.beanvalidation.LocalValidatorFactoryBean;

import javax.sql.DataSource;
import java.io.IOException;

/**
 * @author honglei
 * @version 1.0
 * @date 2020/10/26 15:35
 * @Description
 */
@Configuration
@EnableTransactionManagement
@MapperScan(value = {"moyangjia.retailers.**.mapper","moyangjia.retailers.**.dao"} )
public class SqlConfig implements TransactionManagementConfigurer {



        /**
         * 创建DruidDataSource数据源
         *
         * @return
         */
        @Bean
        @ConfigurationProperties(prefix = "spring.datasource")
        public DataSource dataSource() {
            return DataSourceBuilder.create().type(DruidDataSource.class).build();
        }

        /**
         * 实例化数据源事务管理器
         *
         * @return
         */
        @Bean
        public PlatformTransactionManager txManager() {
            return new DataSourceTransactionManager(dataSource());
        }

        /**
         * 指定默认数据源事务管理器，可扩展多数据源
         */

        @Override
        public PlatformTransactionManager annotationDrivenTransactionManager() {
            return txManager();
        }

        /**
         * 创建Mybatis的SqlSessionFactory，共享在Spring应用程序上下文，然后通过依赖注入将SqlSessionFactory传递给DAO
         *
         * @return
         * @throws IOException
         */
        @Bean
        public MybatisSqlSessionFactoryBean sqlSessionFactory() throws IOException {
            MybatisSqlSessionFactoryBean sqlSessionFactoryBean = new MybatisSqlSessionFactoryBean();
            sqlSessionFactoryBean.setDataSource(dataSource());
            MybatisConfiguration configuration = new MybatisConfiguration();
            configuration.setCallSettersOnNulls(true);
            sqlSessionFactoryBean.setConfiguration(configuration);
            PathMatchingResourcePatternResolver resolver = new PathMatchingResourcePatternResolver();
            sqlSessionFactoryBean.setMapperLocations(resolver.getResources("classpath:mapper/*.xml"));
            sqlSessionFactoryBean.setTypeAliasesPackage("moyangjia.retailers.uims.*.po");
            sqlSessionFactoryBean.setPlugins(new Interceptor[]{new PaginationInterceptor()});
            return sqlSessionFactoryBean;
        }




}
